package com.i.template.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.i.template.constant.RedisConstants;
import com.i.template.constant.ResponseResult;
import com.i.template.domain.PlayListDO;
import com.i.template.domain.vo.PlayListVO;
import com.i.template.service.IPlayListService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lewis
 * @type class
 * @description
 * @since 2024/8/3
 */
@RestController
@RequestMapping("/playlist")
@RequiredArgsConstructor
public class PlayListController {
    private final IPlayListService playListService;
    private final StringRedisTemplate redisTemplate;
    @GetMapping("/{id}")
    public ResponseResult<PlayListVO> getPlayListById(@PathVariable Long id) {
        String strVal = redisTemplate.opsForValue().get(RedisConstants.PLAYLIST_ID_PREFIX + id);
        if(StringUtils.isNotBlank(strVal)) {
            return ResponseResult.ok(JSONUtil.toBean(strVal,PlayListVO.class));
        }
        PlayListVO playListVO = BeanUtil.copyProperties(playListService.queryPlayListById(id), PlayListVO.class);
        redisTemplate.opsForValue().set(RedisConstants.PLAYLIST_ID_PREFIX + id,JSONUtil.toJsonStr(playListVO));
        return ResponseResult.ok(playListVO);
    }
    @GetMapping("/type")
    public ResponseResult<List<PlayListVO>> getPlayListType() {
        String strVal = redisTemplate.opsForValue().get(RedisConstants.PLAYLIST_TYPE_PREFIX);
        if(StringUtils.isNotBlank(strVal)) {
            JSONArray jsonArray = JSONUtil.parseArray(strVal);
            List<PlayListVO> list = JSONUtil.toList(jsonArray, PlayListVO.class);
            return ResponseResult.ok(list);
        }
        List<PlayListVO> playListVOS = BeanUtil.copyToList(playListService.list(), PlayListVO.class);
        redisTemplate.opsForValue().set(RedisConstants.PLAYLIST_TYPE_PREFIX,JSONUtil.toJsonStr(playListVOS));
        return ResponseResult.ok(playListVOS);
    }
    @GetMapping
    public ResponseResult<List<PlayListVO>> listPlayList() {
        String strVal = redisTemplate.opsForValue().get(RedisConstants.PLAYLIST_ALL_PREFIX);
        if(StringUtils.isNotBlank(strVal)) {
            JSONArray jsonArray = JSONUtil.parseArray(strVal);
            List<PlayListVO> list = JSONUtil.toList(jsonArray, PlayListVO.class);
            return ResponseResult.ok(list);
        }
        List<PlayListVO> playListVO = BeanUtil.copyToList(playListService.listQueryPlayList(), PlayListVO.class);
        redisTemplate.opsForValue().set(RedisConstants.PLAYLIST_ALL_PREFIX,JSONUtil.toJsonStr(playListVO));
        return ResponseResult.ok(playListVO);
    }
    @PostMapping
    public ResponseResult<Boolean> savePlayList(@RequestBody PlayListVO playListVO) {
        redisTemplate.delete(RedisConstants.PLAYLIST_ALL_PREFIX);
        return ResponseResult.judge(playListService.save(BeanUtil.copyProperties(playListVO, PlayListDO.class)));
    }
    @PutMapping
    public ResponseResult<Boolean> updatePlayListById(@RequestBody PlayListVO playListVO) {
        boolean flagBySuccess = playListService.updateById(BeanUtil.copyProperties(playListVO, PlayListDO.class));
        if(!flagBySuccess) {
            return ResponseResult.fail();
        }
        redisTemplate.delete(RedisConstants.PLAYLIST_ID_PREFIX + playListVO.getListId());
        return ResponseResult.ok(Boolean.TRUE);
    }
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> deletePlayListById(@PathVariable Long id) {
        boolean removeById = playListService.removeById(id);
        if(!removeById) {
            return ResponseResult.fail();
        }
        redisTemplate.delete(RedisConstants.PLAYLIST_ID_PREFIX + id);
        return ResponseResult.ok(true);
    }
}
